package com.exartisan.datamanager;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.XML;

import android.content.Context;
import android.util.Log;
import android.util.SparseArray;

import com.exartisan.bean.Quiz;
import com.exartisan.bean.Survey;
import com.exartisan.utils.file.XmlUtil;

/**
 * The Class SurveyManager.
 */
public class SurveyManager {
	
	/** The instance. */
	private static SurveyManager singletonManager;
	
	/** The survey library. */
	private HashMap<String, Survey> surveyLibrary;

	/**
	 * Instantiates a new survey manager.
	 */
	private SurveyManager() {
//		if (surveyLibrary == null)
		surveyLibrary = new HashMap<String, Survey>();
	}
	
	/**
	 * Singleton pattern's method to get the unique instance of the class 
	 * which has declared for whole application. It's just like a global object.
	 * @return the singleton object of surveyLibrary
	 */
	public static synchronized SurveyManager getInstance(){
		if(singletonManager==null){
			singletonManager = new SurveyManager();
		}
		return singletonManager;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	public Object clone() throws CloneNotSupportedException{
		throw new CloneNotSupportedException();
	}
	
	/**
	 * Get The list of Survey's Name.
	 * @return ArrayList< String >
	 */
//	public ArrayList<String> getListSurveyName() {
//		ArrayList<String> listsurveyName = new ArrayList<String>();
//		for (String key : surveyLibrary.keySet()) {
//			listsurveyName.add(surveyLibrary.get(key).getSurveyName());
//		}
//		return listsurveyName;
//	}
	
	public ArrayList<String> getListFileName() {
	ArrayList<String> listFileName = new ArrayList<String>();
	listFileName.addAll(surveyLibrary.keySet());
	return listFileName;
}
	/**
	 * Get content of the Survey. That is a list of quizs.
	 *
	 * @param surveyName the survey name
	 * @return ArrayList< Quiz >
	 */
	public ArrayList<Quiz> getsurveyContent(String fileName) {
//		String fileName = getFileNameFrom(surveyName, null);
//		if (fileName != null)
		if(surveyLibrary.containsKey(fileName))
			return surveyLibrary.get(fileName).getContent();
		else
			return null;
	}
	
	/**
	 * Gets the file name from.
	 *
	 * @param surveyName the survey name
	 * @param defaultValue the default value
	 * @return the file name from
	 */
//	public String getFileNameFrom(String surveyName, String defaultValue) {
//		String fileName = defaultValue;
//		for (String key : surveyLibrary.keySet()) {
//			if (surveyLibrary.get(key).getSurveyName().equals(surveyName)) {
//				fileName = key;
//				break;
//			}
//		}
//		return fileName;
//	}
	
	/**
	 * Get an survey name from a file name.
	 *
	 * @param fileName the file name
	 * @return String
	 */
//	public String getsurveyNameFrom(String fileName) {
//		return fileName.substring(0, fileName.indexOf("."));
//	}
	
	public String getSurveyName(String fileName) {
		if(containsKey(fileName)){
			return surveyLibrary.get(fileName).getSurveyName();
		} else return null;
	}
	
	/**
	 * Delete the survey from survey Library.
	 * @param fileName - key of the Library. The library manages data by key.<br> Eg: "test.txt", "document.doc",...
	 * @return true - if adding success <br>
	 * false - if adding failure
	 */
	public boolean deletesurvey(String fileName) {
		if (surveyLibrary.containsKey(fileName)) {
			surveyLibrary.remove(fileName);
			return true;
		} else
			return false;
	}

	/**
	 * Add An survey into survey Library.
	 *
	 * @param folderName <br> eg: "Test", "Folder",...
	 * @param fileName - key of the Library. The library manages data by key.<br> Eg: "test.txt", "document.doc",...
	 * @return true - if adding success <br>
	 * false - if adding failure
	 */
	public boolean addSurvey(String fileName, ArrayList<Quiz> surveyContent) {
		boolean added = false;
		surveyLibrary.put(fileName, new Survey(fileName, surveyContent));
		return added;
	}
	
	/**
	 * Adds survey from a XML file in assets.
	 *
	 * @param context the context
	 * @param fileName name of the XML file
	 */
//	public void addSurveyFromAssets(Context context, String fileName){
//		String surveyName;
//		ArrayList<Quiz> content = new ArrayList<Quiz>();
//		JSONObject data;
//		SparseArray<ArrayList<String>> answers = new SparseArray<ArrayList<String>>();
//		JSONArray qItem;
//		JSONArray ansItem;
////		surveyName = getsurveyNameFrom(fileName);
//		data = XML.toJSONObject(XmlUtil.readFromAssets(context, fileName));
//		surveyName = data.getJSONObject("Survey").getString("Name");
//		qItem = data.getJSONObject("Survey").getJSONObject("Question").getJSONArray("Item");
//		ansItem = data.getJSONObject("Survey").getJSONObject("Answer").getJSONArray("Item");
//		
//		for (int j = 0; j < ansItem.length(); j++){
//			String answer = ansItem.getJSONObject(j).getString("Content");
//			int questionId = ansItem.getJSONObject(j).getInt("QuestionID");
//			if (answers.get(questionId) != null){
//				answers.get(questionId).add(answer);
//			} else {
//				ArrayList<String> tmpAns = new ArrayList<String>();
//				tmpAns.add(answer);
//				answers.put(questionId, tmpAns);
//			}
//		}
//		
//		for (int i = 0; i < qItem.length(); i++){
//			int id = qItem.getJSONObject(i).getInt("id");
//		    String question = qItem.getJSONObject(i).getString("Content");
//			int categoryId = qItem.getJSONObject(i).getInt("CategoryID");
//			if (answers.get(id) != null)
//			content.add(new Quiz(question, categoryId, answers.get(id)));
//			else
//				content.add(new Quiz(question, categoryId, new ArrayList<String>()));	
//		}
//		
//		surveyLibrary.put(fileName, new Survey(surveyName, content));
//	}

	/**
	 * Call this method if you want to modify content of an survey.
	 * @param folderName <br> eg: "Test", "Folder",...
	 * @param fileName - key of the Library. The library manages data by key.<br> Eg: "test.txt", "document.doc",...
	 * @return true - if adding success <br>
	 * false - if adding failure
	 */
	public boolean updatesurvey(String fileName, ArrayList<Quiz> surveyContent) {
		if (containsKey(fileName)==true) {
			deletesurvey(fileName);
			addSurvey(fileName, surveyContent);
			return true;
		} else return false;
		
	}
	
	/**
	 * Check whether the library contains the key or not.
	 *
	 * @param fileName the file name
	 * @return true, if successful
	 */
	public boolean containsKey(String fileName) {
		if (surveyLibrary.containsKey(fileName))
			return true;
		else
			return false;
	}

}
